package pkg137;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ServerApp extends JFrame implements ActionListener,Runnable {
    ServerSocket s;
    Socket s1;
    BufferedReader br;
    BufferedWriter bw;
    TextField text;
    Button button1,button2;
    List list;
    static int portNum;
    static String username;
    
    @Override
    public void run(){
        try{s1.setSoTimeout(1);}catch(Exception e){}
	while (true){
            try{
                list.addItem(br.readLine());
		}
            catch (Exception h){}
        }
    }
    
    public static void main(String arg[]){
        getUsername();	
    }
	
    public static void getUsername(){
        username = JOptionPane.showInputDialog(null,"Enter Server Username: ","Input",JOptionPane.QUESTION_MESSAGE);
	
        if(username==null) System.exit(0);
	else getPort();
    }
	

    public static void getPort(){
        int temp = 0;
	
        do{
            String port = JOptionPane.showInputDialog(null,"Port Number: ","Input Port",JOptionPane.QUESTION_MESSAGE);
			
            if(port==null) System.exit(0);
            else{
                Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
        	Matcher m = p.matcher(port);
                    
                if (m.find()){
                    JOptionPane.showMessageDialog(null, "Please enter only number.","Try again",JOptionPane.ERROR_MESSAGE);
        	}
        	else{
                    temp =1;
                    portNum = Integer.parseInt(port);
                    ServerApp serverApp = new ServerApp("ServerApp_" + username);
        	}
             }
          }
        while(temp == 0);
    }
    
	public ServerApp(final String m){
            super(m);
            setSize(350,350);
            setLocation(200,200);
            setResizable(false);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new BorderLayout());
            button1 = new Button("Send");
            button1.setBackground(Color.gray);
            button2 = new Button("Exit");
            button2.setBackground(Color.gray);
            button1.addActionListener(this);
            button2.addActionListener(this);
            list = new List();
            list.setBackground(Color.black);
            list.setForeground(Color.white);
            text = new TextField();
            
            
            text.addKeyListener(new KeyAdapter(){
          
            public void keyPressed(KeyEvent e){
                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    try{
                    bw.write(username +" said: "+text.getText());
                    text.setText("");
                    bw.newLine();bw.flush();
                    }catch(Exception m){}
             }
                else if (e.getKeyCode()==KeyEvent.VK_ESCAPE){
                    System.exit(0);
             }
           }});

            add(list,"Center");
            add(button1,"West");
            add(button2,"East");		
            add(text,"South");
            setVisible(true);
		
            try{
                s = new ServerSocket(portNum);
                s1=s.accept();
		br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
		bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
		bw.write("Hello " + username + "!" );bw.newLine();bw.flush();
		
                Thread th;
                th = new Thread(this);
                th.start();
			 
            }catch(Exception e){}
	}
        
    @Override
	public void actionPerformed ( ActionEvent e){
        
        if (e.getSource().equals(button2))System.exit(0);
        else{
            try{
                bw.write(text.getText());
		bw.newLine();bw.flush();
            }catch(Exception m){}
          }			  
	}
}